<template>
  <div class="search-input">
    <div class="search-bar">
      <el-input
        v-model="localQuery"
        placeholder="输入搜索关键词..."
        size="large"
        clearable
        @keyup.enter="handleSearch"
        @clear="handleClear"
        class="search-input-field"
      >
        <template #prepend>
          <el-select
            v-model="localSearchType"
            placeholder="搜索类型"
            style="width: 120px"
            @change="handleSearchTypeChange"
          >
            <el-option label="文件名" value="filename" />
            <el-option label="文件内容" value="content" />
            <el-option label="高级搜索" value="advanced" />
          </el-select>
        </template>
        <template #append>
          <el-button
            type="primary"
            :loading="isSearching"
            @click="handleSearch"
            :disabled="!localQuery.trim()"
          >
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
        </template>
      </el-input>
    </div>

    <!-- 搜索历史下拉 -->
    <div class="search-history" v-if="showHistory && recentSearches.length > 0">
      <el-dropdown trigger="click" @command="handleHistorySelect">
        <el-button text type="info" size="small">
          <el-icon><Clock /></el-icon>
          搜索历史
          <el-icon class="el-icon--right"><ArrowDown /></el-icon>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item
              v-for="(historyItem, index) in recentSearches"
              :key="index"
              :command="historyItem"
              class="history-item"
            >
              <div class="history-content">
                <span class="history-query">{{ historyItem }}</span>
                <el-button
                  text
                  size="small"
                  type="danger"
                  @click.stop="handleRemoveHistory(historyItem)"
                >
                  <el-icon><Close /></el-icon>
                </el-button>
              </div>
            </el-dropdown-item>
            <el-dropdown-item divided command="clear-all">
              <el-button text type="danger" size="small">
                <el-icon><Delete /></el-icon>
                清空历史
              </el-button>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>

    <!-- 快速搜索建议 -->
    <div class="quick-search" v-if="showQuickSearch">
      <div class="quick-search-title">快速搜索:</div>
      <el-tag
        v-for="suggestion in quickSearchSuggestions"
        :key="suggestion.label"
        class="quick-search-tag"
        @click="handleQuickSearch(suggestion)"
        effect="plain"
        type="info"
      >
        {{ suggestion.label }}
      </el-tag>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { Search, Clock, ArrowDown, Close, Delete } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

interface QuickSearchSuggestion {
  label: string
  query: string
  type: 'filename' | 'content' | 'advanced'
}

interface Props {
  query: string
  searchType: 'filename' | 'content' | 'advanced'
  isSearching: boolean
  recentSearches: string[]
}

interface Emits {
  (e: 'update:query', value: string): void
  (e: 'update:searchType', value: 'filename' | 'content' | 'advanced'): void
  (e: 'search', query: string): void
  (e: 'clear'): void
  (e: 'clear-history'): void
  (e: 'remove-history', item: string): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const localQuery = ref(props.query)
const localSearchType = ref(props.searchType)
const showHistory = ref(false)
const showQuickSearch = ref(true)

// 快速搜索建议
const quickSearchSuggestions: QuickSearchSuggestion[] = [
  { label: '*.vue 文件', query: '*.vue', type: 'filename' },
  { label: '*.ts 文件', query: '*.ts', type: 'filename' },
  { label: '*.js 文件', query: '*.js', type: 'filename' },
  { label: 'component', query: 'component', type: 'content' },
  { label: 'function', query: 'function', type: 'content' },
  { label: 'import', query: 'import', type: 'content' },
  { label: 'export', query: 'export', type: 'content' },
  { label: 'TODO', query: 'TODO', type: 'content' }
]

// 监听 props 变化
watch(() => props.query, (newQuery) => {
  localQuery.value = newQuery
})

watch(() => props.searchType, (newType) => {
  localSearchType.value = newType
})

// 监听本地值变化并同步到父组件
watch(localQuery, (newQuery) => {
  emit('update:query', newQuery)
})

watch(localSearchType, (newType) => {
  emit('update:searchType', newType)
})

// 处理搜索
function handleSearch() {
  if (!localQuery.value.trim()) {
    ElMessage.warning('请输入搜索关键词')
    return
  }
  
  showHistory.value = false
  showQuickSearch.value = false
  emit('search', localQuery.value.trim())
}

// 处理清空
function handleClear() {
  localQuery.value = ''
  showQuickSearch.value = true
  emit('clear')
}

// 处理搜索类型变化
function handleSearchTypeChange() {
  // 如果有查询内容，自动重新搜索
  if (localQuery.value.trim()) {
    handleSearch()
  }
}

// 处理历史记录选择
function handleHistorySelect(command: string) {
  if (command === 'clear-all') {
    emit('clear-history')
    ElMessage.success('搜索历史已清空')
    return
  }
  
  localQuery.value = command
  handleSearch()
}

// 处理删除历史记录
function handleRemoveHistory(historyItem: string) {
  emit('remove-history', historyItem)
  ElMessage.success('已删除该搜索记录')
}

// 处理快速搜索
function handleQuickSearch(suggestion: QuickSearchSuggestion) {
  localQuery.value = suggestion.query
  localSearchType.value = suggestion.type
  handleSearch()
}


</script>

<style scoped>
.search-input {
  width: 100%;
}

.search-bar {
  margin-bottom: 12px;
}

.search-input-field {
  width: 100%;
}

.search-history {
  margin-bottom: 12px;
}

.history-item {
  padding: 0 !important;
}

.history-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 8px 12px;
}

.history-query {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.quick-search {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.quick-search-title {
  font-size: 14px;
  color: var(--el-text-color-regular);
  margin-right: 8px;
}

.quick-search-tag {
  cursor: pointer;
  transition: all 0.2s;
}

.quick-search-tag:hover {
  background-color: var(--el-color-primary-light-8);
  border-color: var(--el-color-primary-light-6);
  color: var(--el-color-primary);
}
</style>